<?php
include_once CF_FUNCTIONS . "CF_support.php";

// This class will define basic query filter class funcitons
// It leaves out any database access specific ones
// it contains the methods and variables for getting the filters and intrepreting them

class CF_FilterQueryBase{

	/**
	* @var string $selfpage References the page (usually $_PHP['SELF']).  This is used for (optional) page specific filtering
	*/
	var $selfpage;
	/**
	* @var string $feildspec describes columns and option
	*/	
	var $fieldspec;
	/**
	* @var string $filterprefix prefix for filtered variables (column name used)
	*/
	var $filterprefix = 'flt_';	
	/**
	* @var array $filters Holds fitlers as array filers[<column>] = <value> filled by get_filter_vals().  This expects filter variables form the $_REQUEST or from the $_SESSION if saved (pass empty to clear).  Uses prefix determined by $filterprefix
	*/
	var $filters; 


	function CF_FilterQueryBasic($fieldspec){
		__construct($fieldspec);
	}
	
	function __construct($fieldspec){
		$this->fieldspec = $fieldspec;	
	}

	
	function get_filterspec(){
		return $this->filters;
	}

	function set_filterspec(&$filters){
		$this->filters = $filters;
	}	
	

	/**
	 * get_filter_vals() will recieve the filter variables from Session or request
     * and set them in the filters array.
     * To clear filter, pass in a request with the filter set to nothing
     * ($variable1=&variable2=)
     * Filters for each column are described in the fieldspec array 
     * fieldspec[<column>][filters]
	 * Where filters is an array of filters:  
	 * $filterspecarray[filterparam][op] = operation  ( others in spec, but not used here)
	 * This funciton flattens this array into one filters[] array, keyed by filterparam
	 * (filterparam must already be unique so it can be passed in requests)
	 * $this->filters[filterparam][var] = column variable to filter
	 * $this->filters[filterparam][var] = current value
	 * $this->filters[filterparam][op] = operation of filter, to later use in query (=,<,>, etc)	 
	 *
	 */
	function get_filter_vals($method = 'req'){
		foreach($this->fieldspec AS $col_name => $colspec){
			$newspec = array();
			// check if filter needs table or alias source	
			if(isset($colspec['source'])){
				$tablesource = $colspec['source'] . '.';
			}else{
				$tablesource = null;
			}
			// check if filter is just set to 1.  if os, make proper spec array for =	
			if(!empty($colspec['filter'])){
				if(!is_array($colspec['filter'])){
					$newspec[$this->filterprefix . $col_name][op] = '=';
					$colspec['filter'] = $newspec;
				}
			
				foreach($colspec['filter'] AS $filterparam => $filterspec){
					$currentvalue = GetPersistent($filterparam,$req);
					$op = $filterspec['op'];
					if(!is_null($currentvalue)){
						$this->filters[$filterparam]['var'] = $col_name;
						$this->filters[$filterparam]['val'] = $currentvalue;
						$this->filters[$filterparam]['op'] = $op;
						if(!is_null($tablesource)){
							$this->filters[$filterparam]['source'] = $tablesource;
						}
						
						//print '<br />' . $col_name . 'FILTER  param: ' .$filterparam . ' variable: ' .  $this->filters[$filterparam]['var'] . ' Value: ' . $this->filters[$filterparam]['val'] . ' OP: ' . $this->filters[$filterparam]['op'];
					}else{
						unset($this->filters[$filtervar]['var']);
						unset($this->filters[$filtervar]['val']);
						unset($this->filters[$filtervar]['op']);
						unset($this->filters[$filtervar]['source']);
						
					}				
				
				}
			}
		}
		return $this->filters;
	}
	
	
	// Return a row of values equal to their equal filter value
	// use for pre-setting in Add forms
	function GetEqSet(){
		if(is_array($this->filters)){
		foreach($this->filters AS $param=>$filter){
			if($filter['op'] == '='){
				$setrow[$filter['var']] = $filter['val'];
			}
		}	
		}
		return $setrow;
	}
	
	
	
	// ?? NEED TO CHECK IF THIS IS USED OR CURRENT
	// This function returns an array of columnnames => value
	// for only filters set for equal
	// since fieldspec is passed, this can be called without object
	// meant for use from processing class
	
	function GetEqualFilters($fieldspec){
		foreach($fieldspec AS $col_name => $colspec){
			if(!empty($colspec['filter'])){
				if(!is_array($colspec['filter'])){
					$newspec[$this->filterprefix . $col_name][op] = '=';
					$colspec['filter'] = $newspec;
				}
			}
			foreach($colspec['filter'] AS $filterparam => $filterspec){
				if($filterspec['op'] = '='){
					$currentvalue = GetPersistent($filterparam,$req);
					if(!is_null($currentvalue)){
						$result[$col_name] = $currentvalue;
					}
				}
			}
		}
		return $result;
	}

	
	
	//Set filter and Session value - generic	
	function set_filter_val($col_name, $filter_value){
		$this->filters[$this->filterprefix . $col_name]['var'] = $col_name;
		$this->filters[$this->filterprefix . $col_name]['val'] = $filter_value;
		$_SESSION[$this->filterprefix . $col_name] = $filter_value;
	}

	//UnSet filter and Session value - generic	
	function un_set_filter_val($col_name){
		unset($this->filters[$this->filterprefix . $col_name]);
		unset($_SESSION[$this->filterprefix . $col_name]);
	}


	/**
	 * make_query_PSfilter will create a  prepare staement placeholder query filter according to fieldspec settings, Request input, and Session data
	 *
	 * $this->filters[filterparam][var] = column variable to filter
	 * $this->filters[filterparam][var] = current value
	 * $this->filters[filterparam][var] = operation of filter, to later use in query (=,<,>, etc)
	 */	 
	function make_query_PSfilter(){
		if(!empty( $this->filters)) {
            $tabledot = '';
			foreach($this->filters AS $filterparam => $filter){
				if(isset($filter['source'])){
					$tabledot = $filter['source'] . '.';
				}
				$flt_text .= " AND $tabledot" . $filter['var'] . ' ' . $filter['op'] . " :FLT" . $filter['var'] . " ";
				
			}	
		}
	
	return $flt_text;
	}
	
	
	function make_PSfilter_bindings(&$stmt){
			if(!empty( $this->filters)) {
            $tabledot = '';
			foreach($this->filters AS $filtername => $filter){
                $currentvar = $filter['var'];
				if(!empty($this->fieldspec[$currentvar]['filter'])){
					if(isset($this->fieldspec[$currentvar]['source'])){
							$tabledot = $this->fieldspec[$currentvar]['source'] . '.';
						}
					
						$bindvar = ':FLT'. $filter['var'];
						$value = $filter['val'];
						if($filter['op'] == 'LIKE' AND $this->keyword_filter == true){
							$value = '%' . $value . '%';
							}							

						$stmt->bindValue($bindvar,$value);
				}
			}	
		}
	}
	

	
	function make_doctrine_filter(&$docquery, $objectname){
		if(!empty( $this->filters)) {
			foreach($this->filters AS $filterparam => $filterspec){
				//$newandwhere = $objectname . '.' . $filterspec['var'] . $filterspec['op'] .  ' ? ';
				//print $newandwhere;
				$value = $filterspec['val'];
				$docquery->andWhere($objectname . '.' . $filterspec['var'] . ' ' . $filterspec['op'] .  ' ? ', array($value));
			}
		}
		return $docquery;
	}

	
}


?>
